﻿using HCQ2_Common;
using HCQ2_Common.Constant;
using HCQ2_Model;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2_Model.VLine.OutputDto;
using HCQ2_Model.VLine.Params;
using HCQ2WebAPI_Logic.BaseAPIController;
using HCQ2WebAPI_Logic.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    ///  问题反馈控制器
    /// </summary>
    public  class VRFeedBackController : BaseWeiXinApiLogic
    {
        #region 0.0 获取问题反馈列表 + WebApiResultJsonModel GetFeedList(FeedListModel model)
        /// <summary>
        ///  获取问题反馈列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetFeedList(FeedListModel model)
        {
            T_User user=null;
            if (!string.IsNullOrEmpty(model.userid))
            {
                user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
                if(user==null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "传递用户信息异常~", "");
                model.user_id = user.user_id;
            }
            List<FeedBackModels> list = operateContext.bllSession.T_VRIssueFeed.GetFeedBackList(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取问题反馈列表~", list);
        }
        #endregion

        #region 1.0 提交问题反馈 + WebApiResultJsonModel SubmitFeed(FeedBackModel model)
        /// <summary>
        ///  提交问题反馈
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SubmitFeed(FeedBackModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //if(model.folder_id<=0)
            //    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "所属菜单id不允许为空！", null);
            //var folder = operateContext.bllSession.T_PageFolder.FindSingle(o => o.folder_id == model.folder_id);
            //if (folder == null)
            //    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前所属菜单不存在或已被删除~", "");
            var user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
            if(user==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "用户信息有误~", "");
            T_VRIssueFeed feed = new T_VRIssueFeed
            {
                user_id = user.user_id,
                folder_id = model.folder_id,
                feed_title = model.feed_title,
                feed_content = model.feed_content,
                feed_phone = model.feed_phone,
                feed_status = 0,
                feed_source = model.feed_source,
                create_id = user.user_id,
                create_name = user.user_name,
                create_date = DateTime.Now,
                update_id = user.user_id,
                update_name = user.user_name,
                update_date = DateTime.Now
            };
            int mark = operateContext.bllSession.T_VRIssueFeed.Add(feed);
            if (mark > 0 && model.picture!=null &&  model.picture.Count>0)
            {
                string pathServer = "/uploadFile/VRFeedFile/" + user.user_guid + "/" + DateTime.Now.ToString("yyyyMMdd") + "/Breviary/",
                    uploadPath = request.MapPath(request.ApplicationPath + pathServer);
                if (!Directory.Exists(uploadPath))
                    Directory.CreateDirectory(uploadPath);
                //添加图片列表
                model.picture.ForEach(o =>
                {
                    if (!o.picFile.Contains(";base64,"))
                        return;
                    string picName = Guid.NewGuid().ToString().Replace("-", ""), 
                    prefixStr = o.picFile.Split(';')[0].ToString().Split('/')[1];
                    byte[] picByte = Convert.FromBase64String(o.picFile.Split(',')[1]);
                    string tempName = ImageHelper.CreateImageFromBytes(uploadPath, picName, picByte, prefixStr);
                    if (!string.IsNullOrEmpty(tempName))
                        operateContext.bllSession.T_VRIssueFeedPicture.Add(new T_VRIssueFeedPicture
                        {
                            feed_id = feed.id,
                            img_url = "~" + pathServer + picName + "." + prefixStr
                        });
                });
            }
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "提交问题反馈成功~", feed.id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "提交问题反馈失败~", "");
        }
        #endregion

        #region 1.1 更新问题反馈状态 + WebApiResultJsonModel ModifyFeedStatus(FeedStatus model)
        /// <summary>
        ///  更新问题反馈状态
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel ModifyFeedStatus(FeedStatus model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if (model.feed_id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题反馈id不允许为空！", null);
            var feed = operateContext.bllSession.T_VRIssueFeed.FindSingle(o => o.id == model.feed_id);
            if (feed == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题反馈记录不存在或已被删除~", "");
            var user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
            if (user == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "用户信息有误~", "");
            T_VRIssueFeed obj = new T_VRIssueFeed
            {
                feed_status=model.feed_status,
                update_id = user.user_id,
                update_name = user.user_name,
                update_date = DateTime.Now
            };
            int mark = operateContext.bllSession.T_VRIssueFeed.Modify(obj,s=>s.id==model.feed_id, "feed_status", "update_id", "update_name", "update_date");
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "更新问题反馈状态成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "更新问题反馈状态失败~", "");
        }
        #endregion

        #region 1.2 提交问题回复 + WebApiResultJsonModel SubmitReply(FeedReplyModel model)
        /// <summary>
        ///  提交问题回复
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SubmitReply(FeedReplyModel model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if (model.feed_id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题反馈id不允许为空！", null);
            var feed = operateContext.bllSession.T_VRIssueFeed.FindSingle(o => o.id == model.feed_id);
            if (feed == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题反馈记录不存在或已被删除~", "");
            var user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
            if (user == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "用户信息有误~", "");
            T_VRIssueFeedReply obj = new T_VRIssueFeedReply
            {
                user_id = user.user_id,
                feed_id=model.feed_id,
                reply_content=model.reply_content,
                reply_date = DateTime.Now
            };
            int mark = operateContext.bllSession.T_VRIssueFeedReply.Add(obj);
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "提交问题反馈成功~", obj.id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "提交问题反馈失败~", "");
        }
        #endregion

        #region 1.3 删除问题回复 + WebApiResultJsonModel DeleteReply(DeleteReplyModel model)
        /// <summary>
        ///  删除问题回复
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel DeleteReply(DeleteReplyModel model)
        {
            if (model.id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题回复id不允许为空！", null);
            var feed = operateContext.bllSession.T_VRIssueFeedReply.FindSingle(o => o.id == model.id);
            if (feed == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "问题回复记录不存在或已被删除~", "");
            int mark = operateContext.bllSession.T_VRIssueFeedReply.Delete(s => s.id == model.id);
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "删除问题回复成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "删除问题回复失败~", "");
        }
        #endregion
    }
}